﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using CKFinder;

public partial class Admin_AddNews : System.Web.UI.Page
{
    CategoryNewsController categoryController = new CategoryNewsController();
    protected void Page_Load(object sender, EventArgs e)
    {

        CKFinder.FileBrowser fb = new FileBrowser();
        fb.BasePath = "../ckfinder/";
        fb.SetupCKEditor(ckeditor);
        if(!IsPostBack)
        {
            ddlCategoryNews.DataSource = categoryController.GetAll();
            ddlCategoryNews.DataTextField = "name";
            ddlCategoryNews.DataValueField = "category_news_id";
            ddlCategoryNews.DataBind();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        NewsController newsController = new NewsController();
        News n = new News();
        n.Title = txtTitle.Text;
        n.Url = txtUrl.Text;
        n.Description = txtDescription.Text;
        n.Content = ckeditor.Text;
        n.DateStart = DateTime.Now;
        n.CategoryNews_id = Convert.ToInt32(ddlCategoryNews.SelectedValue);
        if(ddlStatus.SelectedIndex==0)
        {
            n.Status = true;
        }
        else
        {
            n.Status = false;
        }
        n.Image = news_value.Value;
        if(newsController.Insert(n)>0)
        {
            Response.Write("<script>alert('Thêm tin tức thành công')</script>");
            txtTitle.Text = "";
            txtDescription.Text = "";
            txtUrl.Text = "";
            ddlCategoryNews.ClearSelection();
            ddlStatus.ClearSelection();
            ckeditor.Text = "";
            news_value.Value = "";
        }
        else
        {
            Response.Write("<script>alert('Thêm tin tức thành công')</script>");
            return;
        }
    }
}